package scfw.service;

import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import scfw.controller.FoodController;
import scfw.dao.FoodMapper;
import scfw.dao.OrderItemMapper;
import scfw.dao.SysUserMapper;
import scfw.model.Food;
import scfw.model.OrderItem;
import scfw.model.SysUser;
import scfw.model.UserOrderItems;
import scfw.service.model.FoodSrModel;
import scfw.service.model.UserSrModel;

@Service("foodService")
public class FoodServiceImpl
		implements FoodServiceI
{
	private static final Logger logger = LoggerFactory.getLogger(FoodController.class);

	@Autowired
	private FoodMapper foodMapper;

	@Autowired
	private OrderItemMapper itemMapper;

	@Autowired
	private SysUserMapper userMapper;

	public Food getFoodById(int id) { return this.foodMapper.selectByPrimaryKey(Integer.valueOf(id)); }


	public List<Food> queryFoods(int userId)
	{
		return this.foodMapper.queryFoods(userId);
	}

	public List<OrderItem> todayOrderItems(int userId) {
		return this.itemMapper.todayOrderItems(Integer.valueOf(userId));
	}

	public List<OrderItem> historyOrderItems(int userId) {
		return this.itemMapper.historyOrderItems(Integer.valueOf(userId));
	}

	public int submitFood(FoodSrModel srModel)
	{
		return this.foodMapper.insert(null);
	}

	@Transactional(rollbackFor = Exception.class)
	public int submitFoodOrder(FoodSrModel srModel) throws Exception{
		double totalPrice = 0.0D;
		UserSrModel user = srModel.getUser();
		for (String foodId : srModel.getFoodIds()) {

			Food f = this.foodMapper.selectByPrimaryKey(Integer.valueOf(Integer.parseInt(foodId)));

			totalPrice += f.getFoodPrice().doubleValue();
		}

		SysUser su = new SysUser();
		su.setId(Integer.valueOf(user.getId()));

		if(Double.valueOf(user.getMyBanlance().doubleValue() - totalPrice) <0){
			throw new Exception("你今天超标了啦！");
		}

		user.setMyBanlance(Double.valueOf(user.getMyBanlance().doubleValue() - totalPrice));

		for (String foodId : srModel.getFoodIds()) {
			OrderItem item = new OrderItem();
			item.setUserId(Integer.valueOf(user.getId()));
			item.setUserName(user.getUserName());
			Food f = this.foodMapper.selectByPrimaryKey(Integer.valueOf(Integer.parseInt(foodId)));

			item.setFoodNumber(Integer.valueOf(1));

			item.setFoodName(f.getFoodName());
			item.setFoodPrice(f.getFoodPrice());
			item.setFoodId(Integer.valueOf(Integer.parseInt(foodId)));
			item.setStatus(0);
			item.setIsDelete("0");
			item.setCreateDate(new Date());
			this.itemMapper.insert(item);
		}

		su.setMyBanlance(user.getMyBanlance());
		this.userMapper.updateByPrimaryKeySelective(su);
		return srModel.getFoodIds().length;
	}

	public FoodSrModel editFood(int id)
	{
		return null;
	}

	public int deleteFoodById(int id)
	{
		return 0;
	}

	public List<UserOrderItems> todayAllOrderItems()
	{
		List<UserOrderItems> list = this.foodMapper.todayAllOrderItems();

		for (UserOrderItems item : list) {
			double sumFoodPrice = 0.0D;
			for (OrderItem oi : item.getOis()) {
				sumFoodPrice += oi.getFoodPrice().doubleValue();
			}
			item.setSumFoodPrice(Double.valueOf(sumFoodPrice));
		}

		return list;
	}

	public List<UserOrderItems> historyAllOrderItems() {
		List<UserOrderItems> list = this.foodMapper.historyAllOrderItems();

		for (UserOrderItems item : list) {
			double sumFoodPrice = 0.0D;
			for (OrderItem oi : item.getOis()) {
				sumFoodPrice += oi.getFoodPrice().doubleValue();
			}
			item.setSumFoodPrice(Double.valueOf(sumFoodPrice));
		}

		return list;
	}

	public void cancelOrderItem(int oid, int fid, UserSrModel user)
	{
		this.itemMapper.deleteByPrimaryKey(Integer.valueOf(oid));

		Food f = this.foodMapper.selectByPrimaryKey(Integer.valueOf(fid));

		SysUser su = this.userMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
		double myBanlance = su.getMyBanlance().doubleValue() + f.getFoodPrice().doubleValue();
		su.setMyBanlance(Double.valueOf(myBanlance));

		this.userMapper.updateByPrimaryKeySelective(su);

		user.setMyBanlance(Double.valueOf(myBanlance));
		logger.info("删除订单" + oid + "成功！");
	}

	public void submitOrderItems()
	{
		int status = 1;
		this.itemMapper.updateTodayOrderItemStatus(status);
	}
}